﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace iNet.Media.Imaging
{
	/// <summary>
	/// GIF 影像解碼工作。
	/// </summary>
	public class GifImageDecodeTask : BasicImageDecodeTask
	{
		#region 建構子
		/// <summary>
		/// 初始化新的 GifImageDecodeTask 執行個體。
		/// </summary>
		public GifImageDecodeTask()
			: base(ImageFormats.Gif)
		{ }
		/// <summary>
		/// 初始化新的 GifImageDecodeTask 執行個體。
		/// </summary>
		/// <param name="source">影像儲存來源。</param>
		public GifImageDecodeTask(MediaStorage source)
			: base(ImageFormats.Gif, source, 0, 0)
		{ }
		#endregion
		#region DecodePixelSize
		internal override Size<int> DecodePixelSize(Stream stream)
		{
			//移動讀取位置
			byte[] buffer = new byte[6];
			if (stream.CanSeek)
				stream.Seek(6, SeekOrigin.Current);
			else if (stream.Read(buffer, 0, 6) < 6)
				throw new MediaDecodeFailedException(MediaDecodeFailedReason.InvalidFormat);

			//讀取資料
			if (stream.Read(buffer, 0, 4) < 4)
				throw new MediaDecodeFailedException(MediaDecodeFailedReason.InvalidFormat);
			ushort width = BitConverter.LittleEndian.ToUInt16(buffer, 0);
			ushort height = BitConverter.LittleEndian.ToUInt16(buffer, 2);

			//取得影像大小
			return new Size<int>(width, height);
		}
		#endregion
	}
}
